package com.guigu.ssq.servlet;

import com.google.gson.Gson;
import com.ly.common.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UploadServlet", value = "/upload.do")
@MultipartConfig//文件上传必须的注解配置
public class UploadServlet extends HttpServlet {

private SsqBiz biz =new SsqBiz();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求行编码集
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //接收上传文件
        Part datafile =request.getPart("datafile");
        //获取文件名
        String fileName = datafile.getSubmittedFileName();
        //保存文件
        datafile.write("f:/"+fileName);
        Result result= new Result(1,"上传成功",null);

        //保存数据
        biz.save("f:/"+fileName);
        //设置响应类型
        response.setContentType("application/jason;charset=utf-8");
        //将Java对象转成json字符串
        String json = new Gson().toJson(result);
        //浏览器输出
        response.getWriter().print(json);

    }
}
